<template>
  <div>
    <BaseTop ref="baseTop" :type="type" :orderInfo="orderInfo" />
    <Common ref="common" :is_cost_reduction="is_cost_reduction" />

    <Button type="primary" size="large" class="marginR10" ghost @click="backOrSee">
      <Icon type="md-arrow-round-back" size="18" class="relative" />返回
    </Button>

    <Button type="primary" size="large" @click="daV_go">确认信息无误后-提交</Button>
  </div>
</template>



<script>
import BaseTop from "@$@/_service/baseTop";
import Common from "@$@/_service/common";
import info_edit_do from "@$@/js/$common/info_edit_do";
export default {
  name: "patentDoAllIn",
  components: { BaseTop, Common },
  data() {
    let {
      $route: {
        query: { type, order_id, goodsId }
      }
    } = this;

    let data = {
      type,
      order_id,
      orderInfo: {}, //专利详情 修改订单时用

      is_cost_reduction: false //减缓打钩，不减缓不打钩，true减缓，false不减缓
    };
    return data;
  },
  created() {
    // 这个页面是补充 撰写资料=》一定会存在 order_id 不会无端端进来
    let { 
      order_id,
      $route: {
        query: {
          second//是否已经补充资料过一次，再一次去修改补充资料,true第二次以上进来，false第一次进来
        }
      }
    } = this;

    //专利详情
    $ajax.itwPT(order_id, resData => {
      this.orderInfo = resData;
      if(!!second){
        console.log('second = ',resData)
        let{
          applicant_list,
          inventor_list
        }=resData;

        // 申请人赋值
        info_edit_do.applicant_list_Fun(this,applicant_list);
        
        //发明人赋值
        info_edit_do.FMR_array_Fun(this,inventor_list);

      }

      //数据-赋值
      this.timAssign();
    });
  },

  mounted() {
    this.$refs['baseTop'].suoJ=false;
  },
  methods: {
    //修改专利订单 赋值
    timAssign() {
      let that = this;

      let {
        orderInfo: { order_id, state, is_cost_reduction },
        orderInfo,
        $route: {
          query: {
            type
          }
        }
      } = that;


      //有无减缓
      this.is_cost_reduction = is_cost_reduction;//true减缓，false不减缓

      //订单详情
      $ajax.orderInfo(order_id, resData => {
        // 右上角名字赋值
        that.$refs['baseTop'].goodsName = resData.goods_name;
      });

      //1待付款 或 2已付款待修改 才能修改
      if (state == 1 || state == 2) {
        
        //baseTop 赋值
        info_edit_do.baseTop_to_Fun(that,orderInfo);
        
      } else {
        $app.toast("该订单不能修改", false, () => {
          $app.closeTag(that.$route); // 关闭本页面
        });
      }
    },
    
    // 提交
    daV_go() {
      let that = this;
      let {
        is_cost_reduction:is_cost_A,//true减缓，false不减缓
      }=this;

      let {
        order_id, //订单ID，首次提交为空；有订单ID，就是修改订单；
        goods_id,
        zName1: name,
        attorney_path: { file_path: attorney_path, file_path1: attorney_path1 },
        attorney_name_list
      } = that.$refs['baseTop'];

      //基本信息 判断专用
      if (!$app.pBaseTop_judge(that, name, attorney_path)) return;

      /////////////////////////// 分割线 ///////////////////////////

      let {
        applicant_list, //申请人信息
        inventor_list//发明人
      } = that.$refs['common'];

      //申请人判断专用
      if (!$app.pCom_judge(that, applicant_list, inventor_list)) return;

      applicant_list = JSON.stringify(applicant_list);
      // console.log('申请人列表json串 = ', applicant_list);

      inventor_list = JSON.stringify(inventor_list);
      // console.log('发明人列表json串 = ',inventor_list);

      //来到这里

      let obj = {
        order_id,
        type: 0, //和后台约定好，打包服务，发明、实用新型、外观，都填0
        is_cost_reduction:is_cost_A,//true减缓，false不减缓

        //baseTop
        name,
        goods_id,
        attorney_path, //委托书文件路径
        attorney_path1, //已经批了章

        attorney_name_list, //代理人

        //common
        applicant_list, //申请人列表json串
        inventor_list //发明人列表json串
      };

      // console.log('go = ',obj)
      console.log("专利提交，上传字段 = ", $app.objToArray(1, obj), "\n", obj);

      // 费减资料判断
      let applicant_A = that.$refs['common'].applicant_list;
      let applicant_CL = [];

      for (let i = 0; i < applicant_A.length; i++) {

        applicant_CL.push({
          is_cost_reduction: true, //是否已费减，true 不上传材料了，false 要上传材料

          // 单文件
          org_code_path: "", //组织机构代码证
          income_proof_path: "", //收入证明
          idcard_path: "", //身份证
          business_license_path: "", //营业执照
          taxes_form_cover_path: "", //上一年度企业所得税纳税申报表封面

          //多文件
          a_class_table_path:'' //JSON串数组 A 类主表复印件
        });

        if (is_cost_A && !applicant_A[i].is_cost_reduction) {

          //applicant_A[i].is_cost_reduction => true可以跳过
          applicant_CL[i].is_cost_reduction=false;//是否已费减，true 不上传材料了，false 要上传材料

          let type = applicant_A[i].type;

          if (type == "事业单位" || type == "科研单位" || type == "大专院校") {
            let {
              org_code_path: { file_path: org_code_path }
            } = applicant_A[i];

            if (!$app.judgeData(org_code_path)) {
              that.$Message.error({
                content: "申请人" + (i + 1) + "：组织机构代码证，不能为空"
              });
              return;
            } else {
              applicant_CL[i].org_code_path = org_code_path;
            }
          }

          if (type == "个人") {
            let {
              income_proof_path: { file_path: income_proof_path },
              idcard_path: { file_path: idcard_path }
            } = applicant_A[i];

            if (!$app.judgeData(income_proof_path)) {
              that.$Message.error({
                content: "申请人" + (i + 1) + "：收入证明，不能为空"
              });
              return;
            } else if (!$app.judgeData(idcard_path)) {
              that.$Message.error({
                content: "申请人" + (i + 1) + "：身份证复印件，不能为空"
              });
              return;
            } else {
              applicant_CL[i].income_proof_path = income_proof_path;
              applicant_CL[i].idcard_path = idcard_path;
            }
          }

          if (type == "工矿企业") {
            let {
              business_license_path: { file_path: business_license_path },
              taxes_form_cover_path: { file_path: taxes_form_cover_path },
              a_class_table_path
            } = applicant_A[i];

            // 营业执照
            if (!$app.judgeData(business_license_path)) {
              that.$Message.error({
                content: "申请人" + (i + 1) + "：营业执照，不能为空"
              });
              return;
            } else {
              applicant_CL[i].business_license_path = business_license_path;
            }

            // 下面两个为选填项

            //上一年度企业所得税纳税申报表封面
            if ($app.judgeData(taxes_form_cover_path)) {
              applicant_CL[i].taxes_form_cover_path = taxes_form_cover_path;
            }

            //A 类主表复印件
            if ($app.judgeData(a_class_table_path)) {
              let a_class_table_array = [];
              a_class_table_path.forEach((jar, r) => {
                a_class_table_array.push($app.getVal_filePath(jar));
              });
              applicant_CL[i].a_class_table_path = JSON.stringify(
                a_class_table_array
              ); //格式成json串
            }
          }
        }
      }
      let app_Array = JSON.parse(obj.applicant_list);
      app_Array.forEach((jar, r) => {
        Object.assign(jar, applicant_CL[r]); //对象合并 jar 原型值 会改变
      });

      console.log("申请人 + 费减人资料 = ", app_Array);
      obj.applicant_list = JSON.stringify(app_Array);
      console.log("申请人列表json串 = ", obj.applicant_list);

      console.log('撰写+打包 = obj => ',obj)
      
      $app.gweQRT(that, obj); //打包服务 提交
    },

    // 返回按钮
    backOrSee() {
      $app.backOrSee();
    }
  }
};
</script>

